#include "gui.window.h"

// Constructor
TOB::UIWindow::UIWindow(uint16 x, uint16 y, uint16 w, uint16 h) {
  posx = x;
  posy = y;
  width = (w) ? w : COLS - x;
  height = (h) ? h : LINES - y;
  createWindow();
}


// Destructor
TOB::UIWindow::~UIWindow() {
  dispose();
}


// Init
void TOB::UIWindow::init() {
#ifdef ADVANCED_LANG
  setlocale(LC_CTYPE, "en_US.UTF-8");
#endif
  // Init curses
  initscr();
  keypad(stdscr, TRUE);
  noecho();
  start_color();
  refresh();

  // Init colors
#ifdef ADM
  init_pair(Color::SelectedMenu, 0, 7);
  init_pair(Color::ActiveButton, 0, 6);
  init_pair(Color::Title, 7, 4);
#else
  init_pair(Color::SelectedMenu, 0, 6);
  init_pair(Color::ActiveButton, 7, 4);
  init_pair(Color::Title, 0, 5);
#endif
  init_pair(Color::Default, 7, 0);
  init_pair(Color::InactiveButton, 0, 7);
  init_pair(Color::Link, 7, 4);
  // Plain colors
  init_pair(Color::Red, 1, 0);
  init_pair(Color::Green, 2, 0);
  init_pair(Color::Blue, 4, 0);
  init_pair(Color::Magenta, 5, 0);
  init_pair(Color::Cyan, 6, 0);
  init_pair(Color::Yellow, 3, 0);
  init_pair(Color::White, 7, 0);
}


// End
void TOB::UIWindow::end() {
  endwin();
}


// Dispose window
void TOB::UIWindow::dispose() {
  delwin(window);
}


// Put string
void TOB::UIWindow::putString(String &in) {
  wprintw(window, PRINT_SARG, in.c_str());
  wrefresh(window);
}


// Put char
void TOB::UIWindow::putChar(Char in) {
  wprintw(window, PRINT_CARG, in);
  wrefresh(window);
}


// Get char
Char TOB::UIWindow::getChar() {
#ifdef ADVANCED_LANG
  Char ch;
  get_wch(&ch);
  return ch;
#endif
  return getch();
}


// Set cursor position
void TOB::UIWindow::setCursor(uint16 x, uint16 y) {
  wmove(window, y, x);
  move(posy + y, posx + x);
  wrefresh(window);
}


// Get cursor position
void TOB::UIWindow::getCursor(uint16 &x, uint16 &y) {
  getyx(window, y, x);
}


// Show/hide cursor
void TOB::UIWindow::showCursor(bool ena) {
  curs_set((ena) ? 1 : 0);
}


// Set color
void TOB::UIWindow::setColor(Color::Type color) {
  wcolor_set(window, color, NULL);
}


// Clear line
void TOB::UIWindow::clearLine() {
  wclrtoeol(window);
}


// Clear window
void TOB::UIWindow::clear() {
  werase(window);
}


// Create window
void TOB::UIWindow::createWindow() {
  window = newwin(height, width, posy, posx);
  box(window, 0, 0);
  wclear(window);
  wrefresh(window);
}
